package com.apes.hr.cultivate.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.repository.EmployeeRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 培训计划 考核
 * @author Linwz
 * @create 2019-06-18 10:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CultivatePlanAppraisal.CONST_TABLE_NAME)
public class CultivatePlanAppraisal extends Model {
    public static final String CONST_TABLE_NAME = "hr_cultivate_plan_appraisal";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "培训计划考核",padding = 8)
    @Comment("培训计划考核编码")
    private String id;   //培训计划考核编码

    @OneToMany(mappedBy="cultivatePlanAppraisal",cascade=CascadeType.ALL)
    private Set<CultivatePlanAppraisalItem> cultivatePlanAppraisalItems = new HashSet<>();//培训计划考核明细
    public Set<CultivatePlanAppraisalItem> getCultivatePlanAppraisalItems() {
        if (cultivatePlanAppraisalItems == null) return null;
        for (CultivatePlanAppraisalItem item: cultivatePlanAppraisalItems) {
            item.setCultivatePlanAppraisal(this);
        }
        return cultivatePlanAppraisalItems;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cultivate_plan_id", referencedColumnName = "id")
    @Comment("培训计划")
    private CultivatePlan cultivatePlan; //培训计划

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id", referencedColumnName = "id")
    @Comment("培训课程")
    private Course Course; //培训课程

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cultivate_class", referencedColumnName = "id")
    @Comment("培训类别")
    private Enumeration cultivateClass; //培训类别

    @Column(name="out_cultivate_plan_uid",length = 4000)
    @Comment("外部培训人")
    private String outCultivatePlanUid;  //外部培训人

    @Column(name = "cultivate_plan_place")
    @Comment("培训地点")
    private String cultivatePlanPlace; //培训地点

    @Column(name = "cultivate_plan_list", length = 4000)
    @Comment("内部培训人")
    private String cultivatePlanList;  //内部培训人


    public void setCultivatePlanLists(String cultivatePlanList) {
    }

    @Transient
    private Set<Employee> cultivatePlanSet;

    public Set<Employee> getcultivatePlanSet() {
        if (isWrite() || isLoad()) {
            cultivatePlanSet = getEmployee(this.cultivatePlanList);
        }
        return cultivatePlanSet;
    }
    public void setcultivatePlanSet( Set<Employee> cultivatePlanSet) {
        this.cultivatePlanSet = cultivatePlanSet;
        if (!isWrite()) {
            this.cultivatePlanList = toConvertEmployee(this.cultivatePlanSet);
        }
    }

    @Transient
    private Set<Employee> getEmployee(String source) {
        if (StringUtil.isEmpty(source)) return null;
        Set<Employee> persons = new HashSet<>();
        String[] rows = source.split(";");
        for (String person : rows) {
            if (person.contains("|")) {
                persons.add(SpringManager.getBean(EmployeeRepository.class).findOne(StringUtils.split(person, "|")[0]));
            }
        }
        return persons;
    }

    private String toConvertEmployee(Set<Employee> persons) {
        StringBuffer sb = new StringBuffer();
        for (Employee employee : persons) {
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(employee.getId());
            sb.append("|");
            sb.append(employee.getName());  // 格式 id|description;
        }
        return sb.toString();
    }

    @Column(name = "note",length = 1200)
    @Comment("备注")
    private String note;//备注

//    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
//    @Temporal(TemporalType.DATE)
//    @Column(name = "begin_date")
//    @Comment("计划开始")
//    private Date beginDate;  //计划开始
//
//    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
//    @Temporal(TemporalType.DATE)
//    @Column(name = "end_date")
//    @Comment("计划结束")
//    private Date endDate;  //计划结束
//
//    @Column(name = "objective",length = 4000)
//    @Comment("培训目的")
//    private String objective;   //培训目的


    @Column(name = "state", length = 16)
    @Comment("状态")
    private String state;   //状态

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid; //审批人

    @Column(name = "approve_date")
    @Comment("审批日期")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    private PartyRole deleteId; //删除人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    private Date deleteDate;    // 删除日期

}
